<template>
  <div>
    <el-dialog
      :visible.sync="dialog.add"
      :close-on-click-modal="false"
      title="添加"
      width="80%"
      append-to-body
      @closed="dialogClosed('addForm')"
    >
      <el-form ref="addForm" :model="form" label-width="120px" :rules="validateRules">
        <el-row>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="店铺标题" prop="shopTitle">
              <el-input v-model="form.shopTitle" placeholder="请输入店铺标题" disabled clearable />
              <el-button
                v-if="chooseGoodsShop"
                type="text"
                size="small"
                @click="showJoinSearch({
                searchDialog: 'GoodsShopMain'
              })"
              >选择店铺标题</el-button>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="自提点名称" prop="title">
              <el-input v-model="form.title" placeholder="请输入自提点名称" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="自提点地点" prop="location">
              <el-input v-model="form.location" placeholder="请输入自提点地点" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="经纬度" prop="longitude">
              <el-button
                type="text"
                size="small"
                @click="showBaiduMap({
                searchDialog: 'UserMain'
              })"
              >选择经纬度 :</el-button>
              {{this.form.longitude}} - {{this.form.latitude}}
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="联系电话" prop="phones">
              <el-input type="textarea" v-model="form.phones" placeholder="请输入联系电话" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="上班时间说明" prop="workTimeDesc">
              <el-input v-model="form.workTimeDesc" placeholder="请输入上班时间说明" clearable />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="dialog.add = false">取消</el-button>
        <el-button type="primary" @click="add" :loading="loading.add">添加</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="dialog.edit"
      :close-on-click-modal="false"
      title="修改"
      width="80%"
      append-to-body
      @closed="dialogClosed('editForm')"
    >
      <el-form ref="editForm" :model="form" label-width="120px" :rules="validateRules">
        <el-row>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="店铺标题" prop="shopTitle">
              <el-input v-model="form.shopTitle" placeholder="请输入店铺标题" disabled clearable />
              <el-button
                type="text"
                size="small"
                @click="showJoinSearch({
                searchDialog: 'GoodsShopMain'
              })"
              >选择店铺标题</el-button>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="自提点名称" prop="title">
              <el-input v-model="form.title" placeholder="请输入自提点名称" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="自提点地点" prop="location">
              <el-input v-model="form.location" placeholder="请输入自提点地点" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="经纬度" prop="longitude">
              <el-button
                type="text"
                size="small"
                @click="showBaiduMap({
                searchDialog: 'UserMain'
              })"
              >选择经纬度 :</el-button>
              {{this.form.longitude}} - {{this.form.latitude}}
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="联系电话" prop="phones">
              <el-input type="textarea" v-model="form.phones" placeholder="请输入联系电话" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="上班时间说明" prop="workTimeDesc">
              <el-input v-model="form.workTimeDesc" placeholder="请输入上班时间说明" clearable />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="dialog.edit = false">取消</el-button>
        <el-button type="primary" @click="edit" :loading="loading.edit">修改</el-button>
      </div>
    </el-dialog>
    <!-- 百度地图选择经纬度 -->
    <BaiduMapDialog ref="baiduMapDialog" v-on:confirmLngAndLat="confirmLngAndLat" />
  </div>
</template>

<script>
import * as utils from '@/utils/utils'
import * as ResponseStatus from '@/utils/response-status'
import UploadImage from '@/components/Upload/UploadImage'
import BaiduMapDialog from '@/components/BaiduMapDialog'
import Tinymce from '@/components/Tinymce'
const resUrl = process.env.VUE_APP_RES_URL
export default {
  name: 'GoodsShopPickupStationAddEdit',
  components: {
    UploadImage,
    Tinymce,
    BaiduMapDialog,
  },
  data() {
    return {
      dialog: {
        add: false,
        edit: false,
      },
      loading: {
        add: false,
        edit: false,
      },
      form: {
        shopId: undefined,
        shopTitle: undefined,
        shopIsActive: undefined,
        title: undefined,
        location: undefined,
        longitude: undefined,
        latitude: undefined,
        phones: undefined,
        workTimeDesc: undefined,
      },
      validateRules: {
        shopId: [
          {
            type: 'integer',
            required: true,
            message: '此项为必须项',
            trigger: 'blur, change',
          },
        ],
        shopTitle: [
          {
            type: 'string',
            required: true,
            message: '此项为必须项',
            trigger: 'blur',
          },
          {
            type: 'string',
            min: 1,
            max: 50,
            message: '必须1-50个字符',
            trigger: 'blur',
          },
        ],
        shopIsActive: [
          {
            type: 'integer',
            required: true,
            message: '此项为必须项',
            trigger: 'blur, change',
          },
        ],
        title: [
          {
            type: 'string',
            required: true,
            message: '此项为必须项',
            trigger: 'blur',
          },
          {
            type: 'string',
            min: 1,
            max: 50,
            message: '必须1-50个字符',
            trigger: 'blur',
          },
        ],
        location: [
          {
            type: 'string',
            required: true,
            message: '此项为必须项',
            trigger: 'blur',
          },
          {
            type: 'string',
            min: 1,
            max: 100,
            message: '必须1-100个字符',
            trigger: 'blur',
          },
        ],
        phones: [
          {
            type: 'string',
            min: 1,
            max: 255,
            message: '必须1-255个字符',
            trigger: 'blur',
          },
        ],
        workTimeDesc: [
          {
            type: 'string',
            min: 1,
            max: 50,
            message: '必须1-50个字符',
            trigger: 'blur',
          },
        ],
      },
      dataDictCodes: ['YES_NO'],
      selects: {
        shopIsActiveSelect: [],
      },
      chooseGoodsShop: true,
      excludeResetProps: [],
      resUrl: resUrl,
    }
  },
  computed: {},
  mounted() {
    this.initDataDict()
  },
  methods: {
    initDataDict() {
      utils
        .getDataDict(this.dataDictCodes)
        .then((response) => {
          if (response) {
          }
        })
        .catch((error) => {})
    },
    dialogClosed(formRef) {
      if (this.excludeResetProps.length > 0) {
        // 有不需要被清空的数据
        let oldForm = JSON.parse(JSON.stringify(this.form))
        this.$refs[formRef].resetFields()
        for (let key in this.form) {
          this.form[key] = undefined
        }
        this.excludeResetProps.forEach((key) => {
          this.form[key] = oldForm[key]
        })
      } else {
        this.$refs[formRef].resetFields()
        for (let key in this.form) {
          this.form[key] = undefined
        }
      }
    },
    add() {
      this.$emit('add')
    },
    edit() {
      this.$emit('edit')
    },
    /** 触发父组件展示关联表数据的搜索对话框 */
    showJoinSearch(options) {
      this.$emit('showJoinSearchDialog', options)
    },
    imgUploadSuccess(formProp, url) {
      this.form[formProp] = url
    },
    imgRemoveSuccess(formProp) {
      this.form[formProp] = undefined
    },
    /**
     * 显示百度地图的模态窗
     */
    showBaiduMap() {
      let baiduMapDialog = this.$refs.baiduMapDialog
      if (this.form.longitude && this.form.latitude) {
        baiduMapDialog.center.lat = this.form.latitude
        baiduMapDialog.center.lng = this.form.longitude
      }
      baiduMapDialog.mapDialog = true
    },
    /**
     * 确认选择经纬度
     */
    confirmLngAndLat(lng, lat) {
      this.form.longitude = lng
      this.form.latitude = lat
    },
  },
}
</script>

<style>
</style>